Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

change-emitter

Package Overview
Dependencies
Maintainers
2
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

change-emitter

Listen for changes. Like an event emitter that only emits a single event type. Really tiny.

  • 0.1.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
509K
decreased by-17.06%
Maintainers
2
Weekly downloads
 
Created
Source

change-emitter

build status npm version

Listen for changes. Like an event emitter that only emits a single event type. Really tiny.

I extracted this from Redux's createStore() because I found it to be useful in other contexts. Use it where you want the most minimal event subscription implementation possible.

Usage

import { createChangeEmitter } from 'change-emitter'

const emitter = createChangeEmitter()

// Called `listen` instead of `subscribe` to avoid confusion with observable spec
const unlisten = emitter.listen((...args) => {
  console.log(args)
})

emitter.emit(1, 2, 3) // logs `[1, 2, 3]`
unlisten()
emitter.emit(4, 5, 6) // doesn't log

Larger example

Here's a (partial) implementation of Redux's createStore:

const createStore = (reducer, initialState) => {
  let state = initialState
  const emitter = createChangeEmitter()

  function dispatch(action) {
    state = reducer(state, action)
    emitter.emit()
    return action
  }

  function getState() {
    return state
  }

  return {
    dispatch,
    getState,
    subscribe: emitter.listen
  }
}

Keywords

FAQs

Package last updated on 17 Apr 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc